﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;
namespace jnxc.Web.flagship
{
    /// <summary>
    /// GetShip 的摘要说明
    /// </summary>
    public class GetShip : IHttpHandler, IRequiresSessionState
    {
        jnxc.BLL.flagship bll = new jnxc.BLL.flagship();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "delete")
                delete(context);
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "getinfo")
                getinfo(context);
        }
        public void getinfo(HttpContext context)
        {


            context.Response.ContentType = "text/plain";
            //===============================================================  
            //获取查询条件:【用户id,开始时间，结束时间，关键字】  
            int pid = 0;
            string pidstr = "", psinfo = "";
            //获取前台传来的值 
            if (null != context.Request.QueryString["fid"])
            {//获取前台传来的值  
                pidstr = context.Request.QueryString["fid"].ToString().Trim();
                pid = int.Parse(pidstr);
            }
            jnxc.Model.flagship model = bll.GetModel(pid);
            if (model != null)
                psinfo = model.fbrief;

            context.Response.Write(psinfo);//返回给前台页面  
            context.Response.End();


        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int level = Web.Admin.myinfo.getLevel(context);
            if (level == 5)
            {
                string fname = context.Request.Form["fname"];
                string flinker = context.Request.Form["flinker"];
                string fphone = context.Request.Form["fphone"];
                string ftel = context.Request.Form["ftel"];
                string fpic = context.Request.Form["fpic"];
                string fadd = context.Request.Form["fadd"];
                string fprovince = context.Request.Form["fprovince"];
                string fcity = context.Request.Form["fcity"];
                string fcounty = context.Request.Form["fcounty"];
                string ftime = context.Request.Form["ftime"];
                string fbrief = context.Request.Form["fbrief"];
                string savepath = "";
                HttpFileCollection files = context.Request.Files;
                if (files.Count == 1)
                {
                    HttpPostedFile file = files[0];
                    if (file.ContentLength > 0)
                    {
                        //全路径  
                        string FullFullName = file.FileName;
                        //获取图片的名称  
                        String fileName = FullFullName.Substring(FullFullName.LastIndexOf("\\") + 1);
                        string exp = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
                        string filenames = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        string path = "~/flagshippics";
                        savepath = "/flagshippics";
                        if (exp == ".jpg" || exp == ".bmp" || exp == ".gif" || exp == ".png")
                        {
                            file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path) + "\\" + filenames + exp);
                            savepath += "/" + filenames + exp;
                        }
                        else
                        {
                            context.Response.Write("P");//返回给前台页面  
                            context.Response.End();

                        }
                    }
                }
                jnxc.Model.flagship model = new jnxc.Model.flagship();
                model.fname = fname;
                model.flinker = flinker;
                model.fphone = fphone;
                model.ftel = ftel;
                model.fbrief = fbrief;
                model.fpic = savepath;
                model.fadd = fadd;
                model.fprovince = fprovince;
                model.fcity = fcity;
                model.fcounty = fcounty;
                model.uid = Web.Admin.myinfo.getID(context);
                if (ftime != "")
                    model.ftime = DateTime.Parse(ftime);
                int rid = bll.Add(model);
                if (rid > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void modify(HttpContext context)
        {
            int level = Web.Admin.myinfo.getLevel(context);
            if (level == 5)
            {
                int fid = 0;
                string htpic = context.Request.Form["htpic"];
                string iidstr = context.Request.Form["fid"];
                if (iidstr != "") fid = int.Parse(iidstr);
                string fname = context.Request.Form["fname"];
                string flinker = context.Request.Form["flinker"];
                string fphone = context.Request.Form["fphone"];
                string ftel = context.Request.Form["ftel"];
                string fpic = context.Request.Form["fpic"];
                string fadd = context.Request.Form["fadd"];
                string fprovince = context.Request.Form["fprovince"];
                string fcity = context.Request.Form["fcity"];
                string fcounty = context.Request.Form["fcounty"];
                string ftime = context.Request.Form["ftime"];
                string fbrief = context.Request.Form["fbrief"];
                string savepath = "";
                HttpFileCollection files = context.Request.Files;
                if (files.Count == 1)
                {
                    HttpPostedFile file = files[0];
                    if (file.ContentLength > 0)
                    {
                        //全路径  
                        string FullFullName = file.FileName;
                        //获取图片的名称  
                        String fileName = FullFullName.Substring(FullFullName.LastIndexOf("\\") + 1);
                        string exp = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
                        string filenames = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        string path = "~/flagshippics";
                        savepath = "/flagshippics";
                        if (exp == ".jpg" || exp == ".bmp" || exp == ".gif" || exp == ".png")
                        {
                            file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path) + "\\" + filenames + exp);
                            savepath += "/" + filenames + exp;
                        }
                        else
                        {
                            context.Response.Write("P");//返回给前台页面  
                            context.Response.End();

                        }
                    }
                    else
                    {
                        savepath = htpic;
                    }
                }
                else
                {
                    savepath = htpic;
                }
                jnxc.Model.flagship model = new jnxc.Model.flagship();
                model.fname = fname;
                model.flinker = flinker;
                model.fphone = fphone;
                model.ftel = ftel;
                model.fbrief = fbrief;
                model.fpic = savepath;
                model.fadd = fadd;
                model.fprovince = fprovince;
                model.fcity = fcity;
                model.fcounty = fcounty;
                model.uid = Web.Admin.myinfo.getID(context);
                model.fid = fid;
                if (ftime != "")
                    model.ftime = DateTime.Parse(ftime);
                //int rid = bll.Add(model);
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int level = Web.Admin.myinfo.getLevel(context);
            if (level == 5)
            {
                int uid = 0;
                string uidstr = context.Request.QueryString["fid"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);
                if (bll.Delete(uid))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                string uname, utruename, uprovince, ucity, ucounty;
                utruename = uname = uprovince = ucity = ucounty = "";
                if (null != context.Request.Form["uname"])
                {
                    uname = context.Request.Form["uname"].ToString().Trim();
                }
                if (null != context.Request.Form["utruename"])
                {
                    utruename = context.Request.Form["utruename"].ToString().Trim();
                }
                if (null != context.Request.Form["uprovince"])
                {
                    uprovince = context.Request.Form["uprovince"].ToString().Trim();
                }
                if (null != context.Request.Form["ucity"])
                {
                    ucity = context.Request.Form["ucity"].ToString().Trim();
                }
                if (null != context.Request.Form["ucounty"])
                {
                    ucounty = context.Request.Form["ucounty"].ToString().Trim();
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                //===================================================================  
                //组合查询语句：条件+排序  
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" fname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" flinker like '%{0}%' and ", utruename);
                }
                if (ucounty != "")
                {
                    strWhere.AppendFormat(" fcounty='{0}' and ", ucounty);
                }
                if (ucity != "")
                {
                    strWhere.AppendFormat(" fcity='{0}' and ", ucity);

                }
                if (uprovince != "")
                {
                    strWhere.AppendFormat(" fprovince='{0}' and ", uprovince);

                }

                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count, 50);//DataSet数据转化为Json数据                
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}